home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / OscillatorSpecifier.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  11.8 KB  |  398 lines  |  [TEXT/KAHL]

  1. /* OscillatorSpecifier.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "OscillatorSpecifier.h"
  31. #include "Memory.h"
  32. #include "SampleSelector.h"
  33. #include "ModulationSpecifier.h"
  34. #include "Envelope.h"
  35. #include "LFOListSpecifier.h"
  36.  
  37.  
  38. struct OscillatorRec
  39.     {
  40.         /* what is the name by which we can refer to this oscillator */
  41.         char*                                            OscillatorName;
  42.  
  43.         /* what kind of oscillator is it */
  44.         OscillatorTypes                        OscillatorType;
  45.  
  46.         /* mapping from pitch to sample */
  47.         struct SampleSelectorRec*    SampleIntervalList;
  48.  
  49.         /* list of oscillators that are modulating us and how */
  50.         struct ModulationSpecRec*    ModulatorInputList;
  51.  
  52.         /* this scales our output so that we can be set relative to other oscillators */
  53.         OscillatorNumType                    OutputLoudness;
  54.  
  55.         /* these are used to make our frequency a multiple of the instrument's */
  56.         /* overall frequency */
  57.         OscillatorNumType                    FrequencyAdjustMultiplier;
  58.         long                                            FrequencyAdjustDivisor;
  59.         OscillatorNumType                    FrequencyAdjustAdder;
  60.  
  61.         /* this switch turns on or off our output, so that we can be used only for */
  62.         /* modulation, if desired.  (True = do use, False = don't use) */
  63.         MyBoolean                                    MixInFinalOutput;
  64.  
  65.         /* this envelope determines the total output level with time */
  66.         struct EnvelopeRec*                LoudnessEnvelope;
  67.  
  68.         /* this LFO list modulates the output of the loudness envelope */
  69.         struct LFOListSpecRec*        LoudnessLFOList;
  70.  
  71.         /* this envelope determines the wave table selection index with time.  this is */
  72.         /* only used for wave table synthesis, not for sampling */
  73.         struct EnvelopeRec*                ExcitationEnvelope;
  74.  
  75.         /* this LFO list modulates the output of the excitation envelope.  it is only */
  76.         /* used for wave table synthesis. */
  77.         struct LFOListSpecRec*        ExcitationLFOList;
  78.  
  79.         /* stereo bias -- fixed amount to move this oscillator left or right by */
  80.         OscillatorNumType                    StereoBias;
  81.         /* surround bias -- fixed amount to move this oscillator front or back by */
  82.         OscillatorNumType                    SurroundBias;
  83.  
  84.         /* time displacement -- how much earlier / later to start sample */
  85.         OscillatorNumType                    TimeDisplacement;
  86.     };
  87.  
  88.  
  89. /* create a new oscillator structure */
  90. OscillatorRec*                        NewOscillatorSpecifier(void)
  91.     {
  92.         OscillatorRec*                    Osc;
  93.  
  94.         Osc = (OscillatorRec*)AllocPtrCanFail(sizeof(OscillatorRec),"OscillatorRec");
  95.         if (Osc == NIL)
  96.             {
  97.              FailurePoint1:
  98.                 return NIL;
  99.             }
  100.         Osc->OscillatorType = eOscillatorSampled; /* default -- this is kinda ugly */
  101.         Osc->OscillatorName = AllocPtrCanFail(0,"OscillatorName");
  102.         if (Osc->OscillatorName == NIL)
  103.             {
  104.              FailurePoint2:
  105.                 ReleasePtr((char*)Osc);
  106.                 goto FailurePoint1;
  107.             }
  108.         Osc->SampleIntervalList = NewSampleSelectorList(0);
  109.         if (Osc->SampleIntervalList == NIL)
  110.             {
  111.              FailurePoint3:
  112.                 ReleasePtr(Osc->OscillatorName);
  113.                 goto FailurePoint2;
  114.             }
  115.         Osc->ModulatorInputList = NewModulationSpecifier();
  116.         if (Osc->ModulatorInputList == NIL)
  117.             {
  118.              FailurePoint4:
  119.                 DisposeSampleSelectorList(Osc->SampleIntervalList);
  120.                 goto FailurePoint3;
  121.             }
  122.         Osc->OutputLoudness = 0;
  123.         Osc->FrequencyAdjustMultiplier = 1;
  124.         Osc->FrequencyAdjustDivisor = 1;
  125.         Osc->FrequencyAdjustAdder = 0;
  126.         Osc->MixInFinalOutput = True;
  127.         Osc->LoudnessEnvelope = NewEnvelope();
  128.         if (Osc->LoudnessEnvelope == NIL)
  129.             {
  130.              FailurePoint5:
  131.                 DisposeModulationSpecifier(Osc->ModulatorInputList);
  132.                 goto FailurePoint4;
  133.             }
  134.         Osc->LoudnessLFOList = NewLFOListSpecifier();
  135.         if (Osc->LoudnessLFOList == NIL)
  136.             {
  137.              FailurePoint6:
  138.                 DisposeEnvelope(Osc->LoudnessEnvelope);
  139.                 goto FailurePoint5;
  140.             }
  141.         Osc->ExcitationEnvelope = NewEnvelope();
  142.         if (Osc->ExcitationEnvelope == NIL)
  143.             {
  144.              FailurePoint7:
  145.                 DisposeLFOListSpecifier(Osc->LoudnessLFOList);
  146.                 goto FailurePoint6;
  147.             }
  148.         Osc->ExcitationLFOList = NewLFOListSpecifier();
  149.         if (Osc->ExcitationLFOList == NIL)
  150.             {
  151.              FailurePoint8:
  152.                 DisposeEnvelope(Osc->ExcitationEnvelope);
  153.                 goto FailurePoint7;
  154.             }
  155.         Osc->StereoBias = 0;
  156.         Osc->TimeDisplacement = 0;
  157.         return Osc;
  158.     }
  159.  
  160.  
  161. /* dispose of an oscillator structure */
  162. void                                            DisposeOscillatorSpecifier(OscillatorRec* Osc)
  163.     {
  164.         CheckPtrExistence(Osc);
  165.         ReleasePtr(Osc->OscillatorName);
  166.         DisposeSampleSelectorList(Osc->SampleIntervalList);
  167.         DisposeModulationSpecifier(Osc->ModulatorInputList);
  168.         DisposeEnvelope(Osc->LoudnessEnvelope);
  169.         DisposeLFOListSpecifier(Osc->LoudnessLFOList);
  170.         DisposeEnvelope(Osc->ExcitationEnvelope);
  171.         DisposeLFOListSpecifier(Osc->ExcitationLFOList);
  172.         ReleasePtr((char*)Osc);
  173.     }
  174.  
  175.  
  176. /* get the actual name of the oscillator.  don't dispose this! */
  177. char*                                            OscillatorGetName(OscillatorRec* Osc)
  178.     {
  179.         CheckPtrExistence(Osc);
  180.         return Osc->OscillatorName;
  181.     }
  182.  
  183.  
  184. /* put a new oscillator name in.  the object becomes owner of the name block */
  185. void                                            PutOscillatorName(OscillatorRec* Osc, char* NewName)
  186.     {
  187.         CheckPtrExistence(Osc);
  188.         CheckPtrExistence(NewName);
  189.         ReleasePtr(Osc->OscillatorName);
  190.         Osc->OscillatorName = NewName;
  191.     }
  192.  
  193.  
  194. /* set the oscillator type */
  195. void                                            OscillatorSetTheType(OscillatorRec* Osc,
  196.                                                         OscillatorTypes WhatKindOfOscillator)
  197.     {
  198.         ERROR((WhatKindOfOscillator != eOscillatorSampled)
  199.             && (WhatKindOfOscillator != eOscillatorWaveTable),PRERR(ForceAbort,
  200.             "NewOscillatorSpecifier:  bad oscillator type number"));
  201.         CheckPtrExistence(Osc);
  202.         Osc->OscillatorType = WhatKindOfOscillator;
  203.     }
  204.  
  205.  
  206. /* find out what kind of oscillator this is */
  207. OscillatorTypes                        OscillatorGetWhatKindItIs(OscillatorRec* Osc)
  208.     {
  209.         CheckPtrExistence(Osc);
  210.         return Osc->OscillatorType;
  211.     }
  212.  
  213.  
  214. /* get the pitch interval --> sample mapping */
  215. struct SampleSelectorRec*    OscillatorGetSampleIntervalList(OscillatorRec* Osc)
  216.     {
  217.         CheckPtrExistence(Osc);
  218.         return Osc->SampleIntervalList;
  219.     }
  220.  
  221.  
  222. /* get the list of oscillators that are modulating us */
  223. struct ModulationSpecRec*    OscillatorGetModulatorInputList(OscillatorRec* Osc)
  224.     {
  225.         CheckPtrExistence(Osc);
  226.         return Osc->ModulatorInputList;
  227.     }
  228.  
  229.  
  230. /* get the output loudness of the oscillator */
  231. OscillatorNumType                    OscillatorGetOutputLoudness(OscillatorRec* Osc)
  232.     {
  233.         CheckPtrExistence(Osc);
  234.         return Osc->OutputLoudness;
  235.     }
  236.  
  237.  
  238. /* put a new output loudness in for the oscillator */
  239. void                                            PutOscillatorNewOutputLoudness(OscillatorRec* Osc,
  240.                                                         double NewOutputLevel)
  241.     {
  242.         CheckPtrExistence(Osc);
  243.         Osc->OutputLoudness = NewOutputLevel;
  244.     }
  245.  
  246.  
  247. /* get the frequency multiplier factor */
  248. OscillatorNumType                    OscillatorGetFrequencyMultiplier(OscillatorRec* Osc)
  249.     {
  250.         CheckPtrExistence(Osc);
  251.         return Osc->FrequencyAdjustMultiplier;
  252.     }
  253.  
  254.  
  255. /* get the frequency divisor integer */
  256. long                                            OscillatorGetFrequencyDivisor(OscillatorRec* Osc)
  257.     {
  258.         CheckPtrExistence(Osc);
  259.         return Osc->FrequencyAdjustDivisor;
  260.     }
  261.  
  262.  
  263. /* get the frequency adder thing */
  264. OscillatorNumType                    OscillatorGetFrequencyAdder(OscillatorRec* Osc)
  265.     {
  266.         CheckPtrExistence(Osc);
  267.         return Osc->FrequencyAdjustAdder;
  268.     }
  269.  
  270.  
  271. /* change the frequency adjust factors */
  272. void                                            PutOscillatorNewFrequencyFactors(OscillatorRec* Osc,
  273.                                                         double NewMultipler, long NewDivisor)
  274.     {
  275.         CheckPtrExistence(Osc);
  276.         Osc->FrequencyAdjustMultiplier = NewMultipler;
  277.         Osc->FrequencyAdjustDivisor = NewDivisor;
  278.     }
  279.  
  280.  
  281. /* put a new frequency adder value */
  282. void                                            PutOscillatorFrequencyAdder(OscillatorRec* Osc,
  283.                                                         double NewAdder)
  284.     {
  285.         CheckPtrExistence(Osc);
  286.         Osc->FrequencyAdjustAdder = NewAdder;
  287.     }
  288.  
  289.  
  290. /* find out if output of this oscillator is to be included in final output */
  291. MyBoolean                                    IncludeOscillatorInFinalOutput(OscillatorRec* Osc)
  292.     {
  293.         CheckPtrExistence(Osc);
  294.         return Osc->MixInFinalOutput;
  295.     }
  296.  
  297.  
  298. /* change whether or not the oscillator is being included in the final output */
  299. void                                            PutOscillatorIncludeInOutputFlag(OscillatorRec* Osc,
  300.                                                         MyBoolean IncludeInOutputFlag)
  301.     {
  302.         CheckPtrExistence(Osc);
  303.         Osc->MixInFinalOutput = IncludeInOutputFlag;
  304.     }
  305.  
  306.  
  307. /* get the loudness envelope for the oscillator */
  308. struct EnvelopeRec*                OscillatorGetLoudnessEnvelope(OscillatorRec* Osc)
  309.     {
  310.         CheckPtrExistence(Osc);
  311.         return Osc->LoudnessEnvelope;
  312.     }
  313.  
  314.  
  315. /* get the list of LFO oscillators modulating the loudness envelope output */
  316. struct LFOListSpecRec*        OscillatorGetLoudnessLFOList(OscillatorRec* Osc)
  317.     {
  318.         CheckPtrExistence(Osc);
  319.         return Osc->LoudnessLFOList;
  320.     }
  321.  
  322.  
  323. /* get the excitation envelope for the oscillator */
  324. struct EnvelopeRec*                OscillatorGetExcitationEnvelope(OscillatorRec* Osc)
  325.     {
  326.         CheckPtrExistence(Osc);
  327.         return Osc->ExcitationEnvelope;
  328.     }
  329.  
  330.  
  331. /* get the list of LFO oscillators modulating the excitation envelope output */
  332. struct LFOListSpecRec*        OscillatorGetExcitationLFOList(OscillatorRec* Osc)
  333.     {
  334.         CheckPtrExistence(Osc);
  335.         return Osc->ExcitationLFOList;
  336.     }
  337.  
  338.  
  339. /* resolve modulator named references to oscillators */
  340. MyBoolean                                    ResolveOscillatorModulators(OscillatorRec* Osc,
  341.                                                         struct OscillatorListRec* ListOfOscillators)
  342.     {
  343.         CheckPtrExistence(Osc);
  344.         CheckPtrExistence(ListOfOscillators);
  345.         return ResolveModulationReferences(Osc->ModulatorInputList,ListOfOscillators);
  346.     }
  347.  
  348.  
  349. /* get the stereo bias factor */
  350. OscillatorNumType                    OscillatorGetStereoBias(OscillatorRec* Osc)
  351.     {
  352.         CheckPtrExistence(Osc);
  353.         return Osc->StereoBias;
  354.     }
  355.  
  356.  
  357. /* put a new value for the stereo bias factor */
  358. void                                            OscillatorPutStereoBias(OscillatorRec* Osc,
  359.                                                         OscillatorNumType NewStereoBias)
  360.     {
  361.         CheckPtrExistence(Osc);
  362.         Osc->StereoBias = NewStereoBias;
  363.     }
  364.  
  365.  
  366. /* get the surround bias factor */
  367. OscillatorNumType                    OscillatorGetSurroundBias(OscillatorRec* Osc)
  368.     {
  369.         CheckPtrExistence(Osc);
  370.         return Osc->SurroundBias;
  371.     }
  372.  
  373.  
  374. /* put a new value for the surround bias factor */
  375. void                                            OscillatorPutSurroundBias(OscillatorRec* Osc,
  376.                                                         OscillatorNumType NewSurroundBias)
  377.     {
  378.         CheckPtrExistence(Osc);
  379.         Osc->SurroundBias = NewSurroundBias;
  380.     }
  381.  
  382.  
  383. /* get the time displacement factor */
  384. OscillatorNumType                    OscillatorGetTimeDisplacement(OscillatorRec* Osc)
  385.     {
  386.         CheckPtrExistence(Osc);
  387.         return Osc->TimeDisplacement;
  388.     }
  389.  
  390.  
  391. /* put a new value for the time displacement factor */
  392. void                                            OscillatorPutTimeDisplacement(OscillatorRec* Osc,
  393.                                                         OscillatorNumType NewTimeDisplacement)
  394.     {
  395.         CheckPtrExistence(Osc);
  396.         Osc->TimeDisplacement = NewTimeDisplacement;
  397.     }
  398.